import {Mesh,PlaneGeometry,MeshBasicMaterial} from 'three';
import UIChar from './ui-char';

export default function UITextManager(Base=class {}){
    return class UITextManager extends Base{
        
        createText(){
            const component = this;
            const textObj = this._buildText();
            textObj.renderOrder = Infinity;
            textObj.onBeforeRender = function() {
                if ( component.updateClippingPlanes ) {
                    component.updateClippingPlanes();
                }
            };
            return textObj;
        }

        _buildText() {
            const textObj=new Mesh(new PlaneGeometry(1e-12,1e-12),new MeshBasicMaterial());
            let max=0;
            let may=0;
            let tmp=0;
            let index=0;
            for(let i=0;i<this.inlines.length;i++){
                const inline=this.inlines[i];
                for(let j=0;j<inline.length;j++){
                    const charObj=inline[j];
                    tmp=charObj.x+charObj.width*0.5;
                    max=max<tmp?tmp:max;
                    tmp=charObj.y-charObj.height*0.5;
                    may=may>tmp?tmp:may;
                    textObj.add(
                    new UIChar(
                        this.getFontMaterial(charObj,index),
                        {
                            width:charObj.width,
                            height:charObj.height,
                            x:charObj.x,
                            y:charObj.y,
                        })
                    );
                    index++;
                }
            }

            textObj.position.set(-max*0.5,-may*0.5,0);
            return textObj;
        }
    

    }
}